package com.zhouqinuan.spring.cloud.develop.framework.demo.mongodb.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.zhouqinuan.spring.cloud.develop.framework.demo.mongodb.req.AddUserDocumentReq;
import com.zhouqinuan.spring.cloud.develop.framework.demo.mongodb.req.UpdateUserDocumentReq;
import com.zhouqinuan.spring.cloud.develop.framework.demo.mongodb.service.UserDocumentService;

import io.swagger.annotations.Api;

@Api(tags = "Mongodb测试例子")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserDocumentService userDocumentService;

    @GetMapping("/getById")
    public String getById(@RequestParam String id) {

        return JSON.toJSONString(userDocumentService.getById(id));
    }

    @GetMapping("/queryByName")
    public String queryByName(@RequestParam String name) {

        return JSON.toJSONString(userDocumentService.queryByName(name));
    }

    @GetMapping("/getByName")
    public String getByName(@RequestParam String name) {

        return JSON.toJSONString(userDocumentService.getByName(name));
    }

    @PostMapping("/addUserDocument")
    public String addUserDocument(@RequestBody AddUserDocumentReq req) {

        return JSON.toJSONString(userDocumentService.addUserDocument(req));
    }

    @PostMapping("/updateUserDocument")
    public String updateUserDocument(@RequestBody UpdateUserDocumentReq req) {

        return JSON.toJSONString(userDocumentService.updateUserDocument(req));
    }

    @PostMapping("/getByPage")
    public String getByPage(@RequestParam(required = false) String name, @RequestParam(required = false, defaultValue = "1") int page, @RequestParam(required = false, defaultValue = "10") int size) {

        return JSON.toJSONString(userDocumentService.getByPage(name, page, size));
    }
}
